<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加/编辑班级详情</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form label-width="100px" ref="editForm" :model="editForm">
          <el-form-item label="年段:" prop="section_id">
            <el-select v-model="editForm.section_id" placeholder="请选择年段" class="w-200">
              <el-option :label="item.section_name" :value="item.id" v-for="(item, index) in sectionList"
                         :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="班级名称:" prop="class_name">
            <el-input auto-complete="off" placeholder="班级名称" class="w-200" v-model="editForm.class_name"></el-input>
          </el-form-item>
          <el-form-item label="排序:" prop="class_name">
            <el-input auto-complete="off" placeholder="排序" class="w-200" v-model="editForm.sort"></el-input>
          </el-form-item>
          <el-form-item label="区域归属:">
            <el-radio-group v-model="editForm.school_id">
              <el-radio :label="item.id" v-for="item in schoolList">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="区域类型:" prop="class_name">
            <el-radio-group v-model="editForm.is_classroom">
              <el-radio :label="0">公共区域</el-radio>
              <el-radio :label="1">班级教室</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定并保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {sectionIndex, classAdd, classDetail, classEdit} from '@/api/admin/class'
import {schoolIndex} from '@/api/admin/system'

export default {
  props: ['visible', 'classId'], // recruitmentType examine 审核
  components: {},
  data() {
    return {
      dialogVisible: false,
      editForm: {
        section_id: '',
        class_name: '',
        map: '',
        is_classroom: 0,
        school_id: 0,
        sort: ''
      }, // 编辑界面数据
      sectionList: [],
      schoolList: []
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.admin_section_index()
        this.schoolIndexFn()
      } else {
        this.sectionList = []
        this.editForm = {
          section_id: '',
          class_name: '',
          map: '',
          is_classroom: 0,
          school_id: 0,
        }
      }
    }
  },
  created() {
  },
  methods: {
    schoolIndexFn() {
      schoolIndex().then(res => {
        let list_ = JSON.parse(JSON.stringify(res.list))
        list_.unshift({id: 0, name: '其他'})
        this.schoolList = list_
      })
    },
    admin_section_index() {
      sectionIndex({p: 1}).then(res => {
        if (res.code === 0) {
          this.sectionList = res.list.data
          if (this.classId) {
            classDetail({id: this.classId}).then(res => {
              console.log('res:', res)
              this.editForm.section_id = res.vo.section_ids
              this.editForm.class_name = res.vo.class_name
              this.editForm.map = res.vo.map
              this.editForm.is_classroom = res.vo.is_classroom
              this.editForm.school_id = res.vo.school_id
              this.editForm.sort = res.vo.sort
            })
          }
        }
      })
    },
    checkEditFn() {
      let data_ = JSON.parse(JSON.stringify(this.editForm))
      if (!data_.section_id) {
        this.$message.error('选择对应年段');
        return
      }
      if (!data_.class_name) {
        this.$message.error('填写班级名称');
        return
      }
      if (!this.classId) {
        classAdd(data_).then(res => {
          if (!res.code) {
            this.$message.success(res.msg)
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        data_.id = this.classId
        classEdit(data_).then(res => {
          if (!res.code) {
            this.$message.success(res.msg)
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
      // this.$emit('checkList', {name: this.itemlable, val: this.infoForm[this.itemlable]})
      // this.closePOpup()
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 500px;
  height: 450px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}
</style>


